<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2022/3/2
 * Time: 14:09
 */
namespace app\index\controller;

use app\index\service\DyOpenService;
use app\middleware\Check;
use think\facade\Db;

class Index{

    protected $middleware = [
        Check::class => ['only' => ['checkUnlock'] ]
    ];

    /**
     * 公告列表
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function noticeList(){
        $list = Db::name('notice')
            ->where('is_deleted',0)
            ->order('create_at desc')
            ->limit(5)
            ->select();
        return success('公告列表',$list);
    }


    /**
     * 考试报名
     * @return \think\response\Json
     */
    public function signUp(){
        validateParam('name,phone,city,platform','name,phone,city,platform');
        $param = input();
        $user_id = null;
        if(isLogin()){
            $user_id = isLogin();
        }
        $data['user_id'] = $user_id;
        $data['name'] = $param['name'];
        $data['phone'] = $param['phone'];
        $data['city'] = $param['city'];//报名的城市
        $data['type'] = 'signUp';
        $data['platform'] = $param['platform'];//平台：wx或者dy
        $data['create_at'] = currentTime();
        Db::name('sign_up_form')->insert($data);
        return success('报名成功');
    }


    /**
     * 首页试卷列表和技巧题库
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function paperList(){
        validateParam('city','city');
        $city = input('city');

        //首先获取地方题
        $cityCount = Db::name('chapter')
            ->where('city_',$city)
            ->sum('total_');

        $nationalCount = Db::name('chapter')
            ->where('city_','0')
            ->sum('total_');

        $list1 = [];
        if($cityCount > 0) {
            $list1[] = [
                'paperName' => $city . '卷',
                'questionCount' => $cityCount,
                'city' => $city
            ];
        }

        $list1[] = [
            'paperName'=>'全国卷',
            'questionCount'=>$nationalCount,
            'city'=>'0'
        ];

        $list2= [];
        //获取技巧题库
        $nationalSkill = Db::name('skill_paper')
            ->where('city_','0')
            ->select()->toArray();
        foreach ($nationalSkill as $k=>$v){
            $v['icon'] = 'https://lvpaiqiche.oss-cn-hangzhou.aliyuncs.com/image/20230526/20230526135004_sy4n.png';
            $list2[] = $v;
        }
        //地方技巧题库
        $citySkill = Db::name('skill_paper')
            ->where('city_',$city)
            ->select();
        foreach ($citySkill as $k=>$v){
            $v['icon'] = 'https://lvpaiqiche.oss-cn-hangzhou.aliyuncs.com/image/20230526/20230526135046_zioo.png';
            $list2[] = $v;
        }
        /*$list2[] = [
            'title_'=>'全国技巧题',
            'sub_title'=>'全套速记，告别死记硬背',
            'city'=>'0',
            'icon'=>'https://lvpaiqiche.oss-cn-hangzhou.aliyuncs.com/image/20230526/20230526135004_sy4n.png',
        ];
        //获取技巧章节是否有
        $chapterCount = Db::name('skill_chapter')
            ->where('city_',$city)
            ->count();
        if($chapterCount > 0) {
            $list2[] = [
                'title_' => $city . '技巧题',
                'sub_title' => '全套速记，告别死记硬背',
                'city' => $city,
                'icon' => 'https://lvpaiqiche.oss-cn-hangzhou.aliyuncs.com/image/20230526/20230526135046_zioo.png'
            ];
        }*/

        return success('首页试卷列表',['paper'=>$list1,'skill'=>$list2]);
    }


    /**
     * 技巧列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function skillPaperList(){
        validateParam('city','city');
        $city = input('city');
        $list2= [];
        //获取技巧题库
        $nationalSkill = Db::name('skill_paper')
            ->where('city_','0')
            ->select()->toArray();
        foreach ($nationalSkill as $k=>$v){
            $v['icon'] = 'https://lvpaiqiche.oss-cn-hangzhou.aliyuncs.com/image/20230526/20230526135004_sy4n.png';
            $list2[] = $v;
        }
        //地方技巧题库
        $citySkill = Db::name('skill_paper')
            ->where('city_',$city)
            ->select();
        foreach ($citySkill as $k=>$v){
            $v['icon'] = 'https://lvpaiqiche.oss-cn-hangzhou.aliyuncs.com/image/20230526/20230526135046_zioo.png';
            $list2[] = $v;
        }
        return success('考试技巧列表',$list2);
    }


    /**
     * 用户题库解锁状态（题库和技巧题库）
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function checkUnlock(){
        validateParam('city','city');
        $city = input('city');
        //地方题库解锁状态
        $record1 = Db::name('user_unlock_region')
            ->field('is_unlock_question,is_unlock_skill')
            ->where('user_id',uid())
            ->where('region_name',$city)
            ->find();
        if(empty($record1)){
            $record1['is_unlock_question'] = 0;
            $record1['is_unlock_skill'] = 0;
        }
        //全国题库解锁状态
        $record2 = Db::name('user_unlock_region')
            ->field('is_unlock_question,is_unlock_skill')
            ->where('user_id',uid())
            ->where('region_name','0')
            ->find();
        if(empty($record2)){
            $record2['is_unlock_question'] = 0;
            $record2['is_unlock_skill'] = 0;
        }
        return success('题库解锁状态',['city'=>$record1,'national'=>$record2]);
    }


    /**
     * 定位获取城市名称
     * @return \think\response\Json
     */
    public function cityName(){
        validateParam('full_name','full_name');
        $region_name = Db::name('region')
            ->where('region_full_name',input('full_name'))
            ->value('region_name');
        if(empty($region_name)){
            $region_name = '杭州';
        }
        return success('定位获取城市名称',$region_name);
    }


    /**
     * 获取所有城市
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function allCity(){
        if(empty(input('keyword'))){
            $region = Db::name('region')
                ->select()->toArray();
            $temp = [];
            foreach ($region as $k => $v) {
                $temp[$v['region_first_letter']][] = $v;
            }
            $temp1 = [];
            foreach ($temp as $k => $v) {
                $temp1[] = [
                    'id' => $k,
                    'data' => $v
                ];
            }
        }else {
            $keyword = input('keyword');
            $region = Db::name('region')
                ->where('region_name','like',"%{$keyword}%")
                ->select()->toArray();
            $temp = [];
            foreach ($region as $k => $v) {
                $temp[$v['region_first_letter']][] = $v;
            }
            $temp1 = [];
            foreach ($temp as $k => $v) {
                $temp1[] = [
                    'id' => $k,
                    'data' => $v
                ];
            }
        }
        return success('所有城市',$temp1);
    }


    /**
     * 热门城市
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function hotCity(){
        $region = Db::name('region')
            ->where('is_hot',1)
            ->select();
        return success('热门城市',$region);
    }


    /**
     * 客服电话
     * @return \think\response\Json
     */
    public function hotLine(){
        $hotLine = Db::name('system_content_setting')
            ->where('key','hotline')
            ->value('value');
        return success('客服电话',$hotLine);
    }

    public function pushHistoryOrderToDy() {
        set_time_limit(0);
        $service = new DyOpenService();
        $orders = Db::name('pay_order')
            ->where('is_pay', 1)
            ->where('is_push_dy', 2)
            ->limit(100)
            ->select();
        foreach ($orders as $order) {
            $user = Db::name('user')
                ->where('id',$order['user_id'])
                ->find();
            $service->pushOrder($user['dy_openid'],$order['order_no'],$order['id'],strtotime($order['create_at']),$order['pay_money'],$order['order_name']);
        }
        echo '处理完成,未处理数量：' . Db::name('pay_order')
                ->where('is_pay', 1)
                ->where('is_push_dy', 0)->count();
    }

    /**
     * 获取所有城市
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function allBrand(){
        $list = Db::name('brands')
            ->field(['id', 'brand_first_letter', 'brand_name'])
            ->select();

        return success('品牌列表', ['list'=>$list]);
    }
}